//
//  QYOrderListVC.m
//  QingYue
//
//  Created by Zheng on 2019/3/26.
//  Copyright © 2019 com.lanou.product_A. All rights reserved.
//

#import "QYOrderListVC.h"
#import "QYOrderListCell.h"
#import "QYOrderDetailVC.h"
#import "QYOrderListModel.h"

@interface QYOrderListVC () <UITableViewDelegate, UITableViewDataSource>
/** 列表*/
@property (nonatomic, strong) UITableView *listTableView;
@property (nonatomic, strong) NSMutableArray *dataArr;

@end

@implementation QYOrderListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单列表页";
    
    [self.listTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(self.view);
    }];

    [self loadListData];
}

- (void)loadListData {
    
    NSDictionary *param = @{@"UserName":[kUserDefaults objectForKey:@"username"]};
    [[YCRequestDataService sharedManager] Post:@"GetOrderDetailsByUserID" parameters:param success:^(id response) {
        if ([response[@"Code"] isEqualToString:@"成功"]) {
            NSArray<NSDictionary *> *list = response[@"OrderDetaillist"];
            
            _dataArr = [NSMutableArray array];

            [list enumerateObjectsUsingBlock:^(NSDictionary * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                QYOrderListModel *model = [[QYOrderListModel alloc] initWithDataDic:obj];
                [_dataArr addObject:model];
            }];

            
            [_listTableView reloadData];

        }
        
    } failure:^{
        kTimeAfter(1, ^{kShowNet(@"网络或服务器错误");});
        
    }];
}

#pragma mark - subviews
- (UITableView *)listTableView
{
    if (!_listTableView) {
        _listTableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.separatorColor = UIColorFromRGB(0xe3e3e3);
        _listTableView.estimatedSectionFooterHeight = 0.0f;
        _listTableView.estimatedSectionHeaderHeight = 0.0f;
        _listTableView.rowHeight = 130;
        _listTableView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag;
        
        if (@available(iOS 11.0, *)) {
            _listTableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        } else {
            self.automaticallyAdjustsScrollViewInsets = NO;
        }
        
        [_listTableView registerClass:[QYOrderListCell class] forCellReuseIdentifier:@"kCellID"];
        
        [self.view addSubview:_listTableView];
    }
    return _listTableView;
}

#pragma mark - tableview delegate and dataSource
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QYOrderListCell *cell = [tableView dequeueReusableCellWithIdentifier:@"kCellID" forIndexPath:indexPath];
    cell.model = _dataArr[indexPath.row];
    return cell;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArr.count;
}


- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return .1f;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return .1f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    QYOrderListModel *model = _dataArr[indexPath.row];
    QYOrderDetailVC *odVC = [[QYOrderDetailVC alloc] init];
    odVC.model = model;
    [self.navigationController pushViewController:odVC animated:YES];
}

@end
